package com.ebn.mation.readagram;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * 列表适配器
 *
 * @date 2024/1/22
 */
public class UserInfoAdapter extends RecyclerView.Adapter<UserInfoAdapter.ViewHolder> {

    private static final String TAG = "UserInfoAdapter";

    private List<UserInfoBean> mUserInfoBeans;
    private IUserOnClick mIUserOnClick;

    public void setIUserOnClick(IUserOnClick userOnClick) {
        mIUserOnClick = userOnClick;
    }

    public UserInfoAdapter(List<UserInfoBean> userInfoBeans) {
        mUserInfoBeans = userInfoBeans;
    }

    @NonNull
    @Override
    public UserInfoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user_info, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UserInfoAdapter.ViewHolder holder, int position) {
        UserInfoBean userInfo = mUserInfoBeans.get(position);
        Log.d(TAG, "onBindViewHolder userInfo: " + userInfo.toString());
        holder.tvName.setText(userInfo.getName());
        holder.tvSubject.setText(userInfo.getSubject());
        holder.tvInsEmail.setText(userInfo.getInstagram());
        holder.tvDescription.setText(userInfo.getFirstComments());

        holder.itemView.setOnClickListener(v -> {
            mIUserOnClick.setUserClick(position);
        });
    }

    @Override
    public int getItemCount() {
        return mUserInfoBeans == null ? 0 : mUserInfoBeans.size();
    }

    public void setUserInfoList(List<UserInfoBean> userInfoList) {
        this.mUserInfoBeans = userInfoList;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvSubject;
        private TextView tvInsEmail;
        private TextView tvDescription;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvSubject = itemView.findViewById(R.id.tv_subject);
            tvInsEmail = itemView.findViewById(R.id.tv_ins_email);
            tvDescription = itemView.findViewById(R.id.tv_description_info);
        }
    }

    public interface IUserOnClick {
        void setUserClick(int position);
    }
}
